package com.zhaoyh.framework.taglib;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateFormatTag extends BodyTagSupport {

	private static final long serialVersionUID = 1L;

	private static String DEFAULT_FORMAT = "yyyy-MM-dd";

	private String format = null;

	private Date timeDate = null;

	private long timeLong = -1L;

	@Override
	public int doEndTag() throws JspException {
		if (timeDate == null && timeLong == -1)
			return SKIP_BODY;
		if (format == null)
			format = DEFAULT_FORMAT;

		try {
			if (timeDate != null)
				pageContext.getOut().print(new SimpleDateFormat(format).format(timeDate));
			else
				pageContext.getOut().print(new SimpleDateFormat(format).format(new Date(timeLong)));
		} catch (IOException e) {
			e.printStackTrace();
		}

		return super.doEndTag();
	}

	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}

	public Date getTimeDate() {
		return timeDate;
	}

	public void setTimeDate(Date timeDate) {
		this.timeDate = timeDate;
	}

	public long getTimeLong() {
		return timeLong;
	}

	public void setTimeLong(long timeLong) {
		this.timeLong = timeLong;
	}
}
